<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            :clickModalClose="true"
            @close="handleClose"
        >
            <el-table
                class="mt-4"
                size="small"
                v-loading="pager.loading"
                :data="pager.lists"
            >
                <el-table-column label="销售人员" prop="nickname" min-width="100" />
                <el-table-column label="是否打卡" prop="clockStatus" min-width="100" />
                <el-table-column label="打卡时间" prop="dateTime" min-width="100" />
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
import {usePaging} from "@/hooks/usePaging";
import {businessLists, clockOnLists} from "@/api/business";
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '打卡记录' : '新增总台账号'
})

const formData = reactive({
    userId: '',
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: clockOnLists,
    params: formData
})

const formRules = {
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await userEdit(data) : await userAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
    getLists();
}

const getDetail = async (row: Record<string, any>) => {
    setFormData({userId : row.id})
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
